package com.imay.live.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.imay.live.R;

/**
 * Created by Administrator on 2017/05/03 0003.
 */

public class IncomeHistView extends LinearLayout {
    Paint paint;
    Path path;
    int left = 0;
    TextView tv_name;
    TextView tv_content;
    TextView tv_end;

    public IncomeHistView(Context context, int left) {
        super(context);
        this.left = left;
        init(context);
    }

    public IncomeHistView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public IncomeHistView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public IncomeHistView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (path == null) {
            path = new Path();
            paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(2);
            paint.setColor(getResources().getColor(R.color.line_ec));
        }
        path.reset();
        path.moveTo(left, canvas.getHeight() - paint.getStrokeWidth());
        path.lineTo(canvas.getWidth(), canvas.getHeight() - paint.getStrokeWidth());
        canvas.drawPath(path, paint);
    }

    private void init(Context context) {
        setBackgroundColor(getResources().getColor(R.color.bg_window));
        tv_name = new TextView(context);
        tv_content = new TextView(context);
        tv_end = new TextView(context);
        tv_name.setTextColor(getResources().getColor(R.color.text_21));
        tv_content.setTextColor(getResources().getColor(R.color.text_75));
        tv_end.setTextColor(getResources().getColor(R.color.text_50));
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER_VERTICAL;
        params.weight = 3;
        params.leftMargin = (int) getResources().getDimension(R.dimen.item_left);
        addView(tv_name, params);


        LayoutParams contentparams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        contentparams.gravity = Gravity.CENTER_VERTICAL;
        contentparams.weight = 3;
        addView(tv_content, contentparams);

        LayoutParams endparams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        endparams.gravity = Gravity.CENTER_VERTICAL;
        endparams.weight = 4;
        addView(tv_end, endparams);
    }

    public void setName(CharSequence text) {
        tv_name.setText(text);
    }

    public void setContent(CharSequence text) {
        tv_content.setText(text);
    }

    public void setEnd(CharSequence text) {
        tv_end.setText(text);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec((int) getResources().getDimension(R.dimen.item_height), MeasureSpec.EXACTLY));
    }
}
